package andestours.test;

  import java.io.FileInputStream;  
  import java.io.FileOutputStream;  
  import java.sql.Connection;  
  import java.sql.Driver;  
  import java.sql.DriverManager;  
  import java.sql.SQLException;  
  import org.dbunit.database.DatabaseConnection;  
  import org.dbunit.database.DatabaseSequenceFilter;  
  import org.dbunit.database.IDatabaseConnection;  
  import org.dbunit.database.QueryDataSet;  
  import org.dbunit.dataset.FilteredDataSet;  
  import org.dbunit.dataset.IDataSet;  
  import org.dbunit.dataset.xml.FlatXmlDataSet;  
  import org.dbunit.dataset.xml.FlatXmlWriter;  
  import org.dbunit.operation.DatabaseOperation;  
    
  public class DBUnitUtils  
    
  {  
    
  public static void generateXML(String driverName,  
                                 String urlDB,  
                                 String userDB,  
                                 String passwordDB,  
                                 String schemaBD,  
                                 String nameXML) throws SQLException {  
        
      Connection conn=null;  
        
      try {  
      // Connect to the database  
      DriverManager.registerDriver((Driver)Class.forName(driverName).newInstance());  
      conn = DriverManager.getConnection(urlDB, userDB, passwordDB);  
      IDatabaseConnection connection = new DatabaseConnection(conn, schemaBD);  
        
    
      DatabaseSequenceFilter filter = new DatabaseSequenceFilter(connection);  
      IDataSet datasetAll = new FilteredDataSet(filter, connection.createDataSet());  
      QueryDataSet partialDataSet = new QueryDataSet(connection);  
    
      String[] listTableNames = filter.getTableNames(datasetAll);  
      for (int i = 0; i < listTableNames.length; i++) {  
          final String tableName = listTableNames[i];  
          // Specify the SQL to run to retrieve the data  
          partialDataSet.addTable(tableName);  
    
      }  
    
      // Specify the location of the flat file(XML)  
      FlatXmlWriter datasetWriter = new FlatXmlWriter(new FileOutputStream("C:\\" + nameXML + ".xml"));  
    
      // Export the data  
      datasetWriter.write(partialDataSet);  
        
      } catch (Exception exc) {  
          exc.printStackTrace();  
        
      } finally{  
          conn.close();  
      }  
  }  
    
  public static void generatePartialXML(String driverName,  
                                        String urlDB,  
                                        String userDB,  
                                        String passwordDB,  
                                        String schemaBD,  
                                        String nameXML) throws SQLException {  
      Connection conn=null;     
    
      try {  
          // Connect to the database  
          DriverManager.registerDriver((Driver)Class.forName(driverName).newInstance());  
          conn = DriverManager.getConnection(urlDB, userDB, passwordDB);  
          IDatabaseConnection connection = new DatabaseConnection(conn, schemaBD);  
    
          QueryDataSet partialDataSet = new QueryDataSet(connection);  
          // Specify the SQL to run to retrieve the data  
          partialDataSet.addTable("fechas_disponibles");  
          partialDataSet.addTable("fechas_viajes");
          partialDataSet.addTable("paises");
          partialDataSet.addTable("reservas");
          partialDataSet.addTable("sitios");
          partialDataSet.addTable("viajes");
          
    
          // Specify the location of the flat file(XML)  
          FlatXmlWriter datasetWriter = new FlatXmlWriter(new FileOutputStream("C:\\" + nameXML + ".xml"));  
    
          // Export the data  
          datasetWriter.write(partialDataSet);  
    
      } catch (Exception exc) {  
          exc.printStackTrace();  
      } finally{  
          conn.close();  
      }  
  }  
    
  public static void createData(String driverName,  
                                    String urlDB,  
                                    String userDB,  
                                    String passworDB,  
                                    String nameXML) throws SQLException {  
    
          Connection conn = null;  
          try {  
              // Connect to the database  
              DriverManager.registerDriver((Driver)Class.forName(driverName).newInstance());  
              conn = DriverManager.getConnection(urlDB, userDB, passworDB);  
              IDatabaseConnection connection = new DatabaseConnection(conn);  
    
  DatabaseOperation.INSERT.execute(connection,new FlatXmlDataSet(new FileInputStream("C:\\" + nameXML + ".xml")));  
        
          } catch (Exception exc) {  
              exc.printStackTrace();  
          } finally{  
              conn.close();  
          }  
  }  
    
  public static void deleteData(String driverName,  
                                String urlDB,  
                                String userDB,  
                                String passworDB,  
                                String nameXML) throws SQLException {  
      Connection conn = null;  
      try {  
      // Connect to the database  
      DriverManager.registerDriver((Driver)Class.forName(driverName).newInstance());  
      conn = DriverManager.getConnection(urlDB, userDB, passworDB);  
      IDatabaseConnection connection = new DatabaseConnection(conn);  
  DatabaseOperation.DELETE.execute(connection, new FlatXmlDataSet(new FileInputStream("C:\\" + nameXML + ".xml")));  
            
      } catch (Exception exc) {  
          exc.printStackTrace();  
      } finally{  
          conn.close();  
      }  
  }  
    
  }  